//
//  MusicPlayerView.m
//  product_B
//
//  Created by zyc on 16/5/31.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MusicPlayerView.h"

@implementation MusicPlayerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//- (instancetype)init{
//    if (self = [super init]) {
//    }
//    return self;
//}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    UserViewController *user = [[UserViewController alloc]init];
    user.modelID = _model.author.user_id;
    if ([self.delegate respondsToSelector:@selector(click:)]) {
        [self.delegate click:user];
    }
}

- (void)configureWithModel:(MusicModel *)model{
    MusicAuthorModel *author = model.author;
    _model = model;
    [_picture sd_setImageWithURL:[NSURL URLWithString:author.web_url]];
    [_button setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
    if (![model.music_id hasPrefix:@"http"]) {
        [_button removeFromSuperview];
    }
    _picture.layer.cornerRadius = 30;
    _picture.layer.masksToBounds = NO;
    _author.text = author.user_name;
    _desc.text = author.desc;
    _title.text = model.title;
    _time.text = [model.maketime substringToIndex:10];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    _picture.userInteractionEnabled = YES;
    [_picture addGestureRecognizer:tap];

}



- (IBAction)buttonAction:(UIButton *)sender {

    
    if (![[MusicPlayerManager shareInstance].playUrl isEqualToString:_model.music_id]) {
        [[MusicPlayerManager shareInstance]replaceItemWithUrlString:_model.music_id];
//        [MusicPlayerManager shareInstance].url = _model.music_id;
        [_button setBackgroundImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
        
        
    }else{
        [[MusicPlayerManager shareInstance]playerPlayAndPause];
        if ([MusicPlayerManager shareInstance].isPlay) {
            [_button setBackgroundImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
        }else{
            [_button setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
        }
    }
    MusicAuthorModel *author = _model.author;
    MusicPlayerModel *musicModel = [[MusicPlayerModel alloc]init];
    musicModel.picture = author.web_url;
    musicModel.author = author.user_name;
    musicModel.title = _model.title;
    [MusicPlayerManager shareInstance].musicModel = musicModel;
    [MusicPlayerManager shareInstance].author = author;
    [MusicPlayerManager shareInstance].picUrl = _model.cover;
    
}







@end
